<script>
import { mapMutations } from "vuex";
import { checkUpdater } from "@/common/checkUpdater";
import utils from "common/utils.js";
export default {
  globalData: {
    categoryId: 0,
  },
  onLaunch: function (o) {
    // #ifdef APP-PLUS
    this.$tabbarView.init(); //画底部发布按钮
    // #endif
    //登录socket
    this.$store.commit("WS_INTERVAL");
    // #ifdef APP-PLUS
    // 锁定屏幕方向
    plus.screen.lockOrientation("portrait-primary"); //锁定
    // #endif
    //console.log('App Launch', o);
    //获取推送消息
    // #ifdef APP-PLUS
    const _self = this;
    const _handlePush = function (message) {
      uni.navigateTo({
        url: "../message/index",
      });
    };
    plus.push.addEventListener("click", function (message) {
      _handlePush(message);
    });
    plus.push.addEventListener("receive", function (message) {
      if (message.payload != "LocalMSG") {
        var options = { cover: false };
        var str = message.content;
        plus.push.createMessage(str, "LocalMSG", options);
      }
    });
    // app打开时先检查是否有更新记录（更新记录会被保存在storage中）
    // 更新记录的内容为 { 更新标记, 安装包位置 }
    // 如果存在更新记录，且更新标记（complete=true）则删除上次使用过的安装包，达到存储释放空间的目的
    //检查更新
    const updated = uni.getStorageSync("updated"); // 尝试读取storage

    if (updated.completed === true) {
      // 如果上次刚更新过
      // 删除安装包及安装记录
      console.log("安装记录被删除，更新成功");
      uni.removeSavedFile({
        filePath: updated.packgePath,
        success: (res) => {
          uni.removeStorageSync("updated");
        },
      });
    } else if (updated.completed === false) {
      uni.removeStorageSync("updated");
      plus.runtime.install(
        updated.packgePath,
        { force: true },
        function () {
          //完成更新向服务器进行通知
          uni.setStorage({
            key: "updated",
            data: {
              completed: true,
              packgePath: updated.packgePath,
            },
            success: (res) => {
              console.log("成功安装上次的更新，应用需要重启才能继续完成");
            },
          });
          //plus.runtime.restart();
          plus.runtime.quit();
        },
        function (err) {
          console.log(JSON.stringify(err));
        }
      );
    } else {
      console.log(this.$current.id);
      // 检查更新，参数：{ 当前版本号，跳转到更新页面的url }
      plus.runtime.getProperty(plus.runtime.appid, function (inf) {
        console.log(JSON.stringify(inf));
        checkUpdater(inf.version, "../update/index");
      });
    }

    // #endif

    // 红包逻辑
    // 定时刷新红包队列
    const func = () => {
      const data = utils.handleHongbao();
      const status = utils.handleHongbaoStatus();

      if (!status && data !== false) {
        utils.handleHongbaoStatus("true");
        uni.navigateTo({
          url: `/pages/hongbao/hongbao?msg=${data}`,
        });
      }

      setTimeout(func, 2000);
    };

    setTimeout(func, 2000);
  },
  onShow: function (o) {
    //console.log('App Show', o);
  },
  onHide: function () {
    console.log("App Hide");
    utils.handleHongbaoStatus("false");
  },
  methods: {
    //...mapMutations(['updatePushMessage']),
  },
};
</script>

<style lang="scss">
@import "uview-ui/index.scss";
page {
  background-color: #fff;
}
.centre {
  text-align: center;
  margin: 200rpx auto;
  font-size: 32rpx;
  color: #999999;
  image {
    width: 327rpx;
    height: 213rpx;
  }
  .tips {
    font-size: 24rpx;
    color: #999999;
    margin-top: 20rpx;
  }
  .btn {
    margin: 80rpx auto;
    width: 200rpx;
    border-radius: 32rpx;
    line-height: 64rpx;
    color: #ffffff;
    font-size: 26rpx;
    background: linear-gradient(
      270deg,
      rgba(249, 116, 90, 1) 0%,
      rgba(255, 158, 1, 1) 100%
    );
  }
}
</style>
